import {DeviceEventEmitter} from 'react-native';
import Stomp from 'stompjs';



let queueName = '';
let routingKey1 = 'e2b6ff53-3cb5-4dc9-95e0-d7cad28fa7e6';
let WebSocketInstance = undefined;
let client = undefined;

const connectStomp = (routingKey) => {
  if (!WebSocketInstance) {
    WebSocketInstance = new WebSocket("ws://messa.com:8080/ws");
  }

  queueName = "stomp-subscription" + uuid();
  client = Stomp.over(WebSocketInstance);


  client.connect(
    "message_user_ro", "nYXPFND2",
    () => {

      console.log("链接成功");

      client.subscribe('/exchange/message_fanout_exchange', function (d) {

        console.log("message_fanout_exchange:" + d.body);

      }, {'x-queue-name': queueName});


      // client.subscribe('/exchange/message_topic_exchange/' + routingKey, function (d) {
      //   console.log("message_topic_exchange:" + d.body);
      // }, {'x-queue-name': queueName});

      client.subscribe('/exchange/message_topic_exchange/' + routingKey1, function (d) {

        console.log("message_topic_exchange:" + d.body);

      }, {'x-queue-name': queueName});

    },
    () => {
      console.log("链接失败")
    }, '/'
  );
};


export {connectStomp};
